package com.yc.bdw.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yc.bdw.Adapter.AddressListAdapter;
import com.yc.bdw.R;
import com.yc.bdw.Utils.Constant;
import com.yc.bdw.model.Address;
import com.yc.bdw.Widget.CustomToast;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.ui.BindView;

import java.util.ArrayList;
import java.util.List;

public class ReceiverActivity extends KJActivity {
    @BindView(id = R.id.address_back, click = true)
    private ImageView backBtn;
    @BindView(id = R.id.address_add, click = true)
    private ImageView addBtn;
    @BindView(id = R.id.address_ok, click = true)
    private TextView okBtn;
    @BindView(id = R.id.address_list, click = true)
    private ListView addressList;

    private List<Address> addresses = new ArrayList<Address>();
    private AddressListAdapter adapter;

    @Override
    public void setRootView() {
        setContentView(R.layout.activity_address_list);
    }

    @Override
    public void initData() {
        super.initData();

        init();

        adapter = new AddressListAdapter(aty);
        adapter.setList(addresses);
        adapter.init();
    }

    private void init() {
        Address item = new Address();
        item.setName("双");
        item.setAddress("新疆乌鲁木齐新市区");
        item.setDetailAddress("创业大厦1105");
        item.setPhone("1235469853");
        addresses.add(item);

        Address item2 = new Address();
        item2.setName("静");
        item2.setAddress("新疆乌鲁木齐新市区");
        item2.setDetailAddress("创业大厦1105");
        item2.setPhone("1235469853");
        addresses.add(item2);

        Address item3 = new Address();
        item3.setName("君");
        item3.setAddress("新疆乌鲁木齐新市区");
        item3.setDetailAddress("创业大厦1105");
        item3.setPhone("1235469853");
        item3.setIsDefault(true);
        addresses.add(item3);

        Address item4 = new Address();
        item4.setName("婷");
        item4.setAddress("新疆乌鲁木齐新市区");
        item4.setDetailAddress("创业大厦1105");
        item4.setPhone("1235469853");
        addresses.add(item4);

        Address item5 = new Address();
        item5.setName("嘉");
        item5.setAddress("新疆乌鲁木齐新市区");
        item5.setDetailAddress("创业大厦1105");
        item5.setPhone("1235469853");
        addresses.add(item5);
    }

    @Override
    public void initWidget() {
        super.initWidget();

        addressList = (ListView) findViewById(R.id.address_list);
        addressList.setAdapter(adapter);
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()) {
            case R.id.address_back:
                finish();
                break;
            case R.id.address_add:
                break;
            case R.id.address_ok:
                int position = adapter.getWhich();
                if ( position == -1 ) {
                    CustomToast.showToast(aty, R.string.noPayment, Constant.toastDuring);
                } else {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putString("name",addresses.get(position).getName());
                    bundle.putString("phone",addresses.get(position).getPhone());
                    bundle.putString("address",addresses.get(position).getAddress()+addresses.get(position).getDetailAddress());
                    intent.putExtras(bundle);
                    setResult(Constant.receiver_ok,intent);
                    finish();
                }
                break;
        }
    }
}
